package com.freedom.common.core.result;

import cn.hutool.core.collection.CollUtil;
import com.freedom.common.core.enums.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class WebPageResult<T> implements Serializable {

    private String code;

    private DataResult<T> data;

    private String msg;

    public static <T> WebPageResult<T> success(List<T> records, Integer pageSize) {
        WebPageResult<T> result = new WebPageResult<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getMsg());

        WebPageResult.DataResult<T> data = new WebPageResult.DataResult<T>();
        data.setRecords(records);
        if (CollUtil.isNotEmpty(records) && records.size() == pageSize) {
            data.setHasNext(Boolean.TRUE);
        }
        result.setData(data);

        return result;
    }

    @Data
    public static class DataResult<T> {

        private List<T> records;

        private boolean hasNext;

    }
}
